package com.leoman.plz_p.presenter;


import com.leoman.plz_p.interfaces.CourseDetailView;
import com.leoman.plz_p.module.api.CourseApi;
import com.leoman.plz_p.module.bean.BaseData;
import com.leoman.plz_p.module.bean.BaseDataWithList;
import com.leoman.plz_p.module.bean.BookListBean;
import com.leoman.plz_p.module.bean.CourseCommentBean;
import com.leoman.plz_p.module.bean.MapData;
import com.leoman.plz_p.module.bean.PageListData;
import com.leoman.plz_p.module.bean.TestBean;
import com.leoman.plz_p.subscriber.ResponseSubscriber;
import com.leoman.plz_p.utils.UserHelper;

import cn.com.corelib.api.ResponseTransformer;
import cn.com.corelib.base.BasePresenter;

public class CourseDetailPresenter extends BasePresenter<CourseDetailView> {

    private CourseApi courseApi;

    @Override
    protected void onViewAttach() {
        super.onViewAttach();
        courseApi=getApi(CourseApi.class);
    }

    public void getRecentStudy(String courseId){
        courseApi.getRecentStudy(UserHelper.getToken(),courseId).compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> mapDataBaseData) {
                        if (checkDataNotNull(mapDataBaseData))
                        view.obtainRecentStudy(mapDataBaseData.data);
                    }
                });
    }

    public void getBookList(String courseId){
        courseApi.getCourseList(UserHelper.getToken(),courseId).compose(new ResponseTransformer<>(this.<BaseDataWithList<BookListBean>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseDataWithList<BookListBean>>(view) {
                    @Override
                    public void success(BaseDataWithList<BookListBean> list) {
                        if (checkListNotNull(list.data)){
                            view.obtainBookList(list.data);
                        }
                    }
                });
    }

    public void getStudyNumber(){
        courseApi.getStudyNumber().compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> mapDataBaseData) {
                        if (checkDataNotNull(mapDataBaseData)){
                            view.obtainStudyNumber(mapDataBaseData.data.number);
                        }
                    }
                });
    }

    public void getCourseExamIds(String courseId,String courseChapterId){
        courseApi.getExamInfo(UserHelper.getToken(),courseChapterId,courseId)
                .compose(new ResponseTransformer<>(this.<BaseData<MapData>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData>>(view) {
                    @Override
                    public void success(BaseData<MapData> mapDataBaseData) {
                        view.obtainExamIds(mapDataBaseData.data.ids,mapDataBaseData.data.examin);
                    }
                });
    }

    public void getComment(String courseId,int page,int pageSize){
        courseApi.getCourseCommentByCourseId(courseId,page,pageSize).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CourseCommentBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CourseCommentBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CourseCommentBean>> listData) {
                        view.obtainCommentList(listData.data.getRows());
                    }

                });
    }

    public void sendComment(String courseId,String content){
        getView().showLoading();
        courseApi.sendCourseCommentByCourseId(UserHelper.getToken(),courseId,UserHelper.getUserId()+"",content)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        view.obtainCommentSuccess();
                    }
                });
    }

    public void getTestList(String courseId){
        courseApi.getTestList(courseId).compose(new ResponseTransformer<>(this.<BaseData<TestBean>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<TestBean>>(view) {
                    @Override
                    public void success(BaseData<TestBean> listData) {
                        view.obtainTestList(listData.data.getList());
                    }

                });
    }


    @Override
    public void onStart() {
        getStudyNumber();
    }
}